﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package net.wooga.pets.controllers {
    import net.wooga.pets.events.*;
    import net.wooga.pets.models.vo.*;
    import net.wooga.pets.models.*;
    import org.robotlegs.mvcs.*;
    import net.wooga.pets.models.world.*;

    public class UpdateShopItemsCommand extends Command {

		[Inject]
        public var shopModel:ShopModel;
		[Inject]
        public var userModel:UserModel;
		[Inject]
        public var gameStateModel:GameStateModel;
		[Inject]
        public var worldModel:WorldModel;
		[Inject]
        public var levelModel:LevelModel;
		[Inject]
        public var sessionModel:SessionModel;

        override public function execute():void{
            var _local2:ShopItemVO;
            if (this.userModel.isVisitFriendsMode()){
                return;
            };
            var _local1:Array = this.shopModel.shopItems;
            for each (_local2 in _local1) {
                this.updateAvailability(_local2);
                this.updateLockedState(_local2);
            };
            this.sortShopItems();
        }
        private function debugFunction_logItems(_arg1:Array):void{
            var _local2:ShopItemVO;
            for each (_local2 in _arg1) {
                if ((((_local2.row == 1)) && ((_local2.category == "objects")))){
                    log(_local2.position);
                    log(_local2.type);
                };
            };
        }
        private function sortShopItems():void{
            this.shopModel.shopItems.sortOn("position", Array.NUMERIC);
        }
        private function updateLockedState(_arg1:ShopItemVO):void{
            var _local2:Boolean = _arg1.isLocked;
            if (_arg1.isFree()){
                _arg1.isLocked = false;
            } else {
                _arg1.isLocked = (((_arg1.count == 0)) && ((this.userModel.coins < _arg1.coins)));
            };
            if (_local2 != _arg1.isLocked){
                dispatch(new ShopEvent(ShopEvent.SHOP_ITEM_LOCKED_CHANGE, _arg1));
            };
        }
        private function updateAvailability(_arg1:ShopItemVO):void{
            var _local2:Boolean = _arg1.available;
            if (_arg1.isRoom()){
                _arg1.available = this.isRoomAvailable(_arg1);
            } else {
                _arg1.available = ((this.isDateInPeriod(_arg1)) || (this.storedItemsExists(_arg1)));
            };
            if (_local2 != _arg1.available){
                dispatch(new ShopEvent(ShopEvent.SHOP_ITEM_AVAILABLE_CHANGE, _arg1));
            };
        }
        private function storedItemsExists(_arg1:ShopItemVO):Boolean{
            return ((_arg1.numberOfItems.length > 0));
        }
        private function isRoomAvailable(_arg1:ShopItemVO):Boolean{
            var _local2:int = this.levelModel.getLevelOfRoomType(_arg1.type);
            var _local3:Boolean = this.worldModel.hasAreaType(_arg1.type);
            if (this.userModel.level < _local2){
                return (false);
            };
            return (!(((_arg1.isOneShotItem) && (_local3))));
        }
        private function isDateInPeriod(_arg1:ShopItemVO):Boolean{
            if (_arg1.availableFrom == ""){
                _arg1.availableFrom = "1970-1-1";
            };
            if (_arg1.availableUntil == ""){
                _arg1.availableUntil = "9999-1-1";
            };
            var _local2:Date = new Date();
            _local2.setTime((this.sessionModel.timestamp * 1000));
            var _local3:Date = this.getDate(_arg1.availableFrom);
            var _local4:Date = this.getDate(_arg1.availableUntil);
            return ((((_local3 <= _local2)) && ((_local2 <= _local4))));
        }
        private function getDate(_arg1:String):Date{
            var _local2:Array = _arg1.split("-");
            return (new Date(_local2[0], (_local2[1] - 1), _local2[2]));
        }

    }
}//package net.wooga.pets.controllers 
